/**
 * author:Administrator
 * date:2015/7/27
 * desc:TODO
 */
"use strict";

var model = require('./models/role.js');

function RoleDao() {
}

var rp = RoleDao.prototype;

rp.saveOne = function*(json) {
    var query = {name: json.roleName};

    var entity = {
        name: json.roleName,
        pUniqueName: json.pUniqueName
    };

    // in case of duplicate groupName:name
    let result;
    result = yield model.findOne(query);
    if (result && result._doc) {
        delete entity.name;
        result = yield model.update(query, entity, {upsert: true, new: true});
    } else {
        result = yield new model(entity).save();
    }


    return result;
};

rp.get = function*(json) {
    // aggregate
    let query = {name: json.name};
    let result = yield model.findOne(query); //get all
    return result && result._doc;
};

module.exports = new RoleDao();